package com.example.zytphone;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.lang.ref.WeakReference;

public class TimeActivity3 extends AppCompatActivity {
    private TextView tv;
    //声明ProgressBar对象
    private ProgressBar bar;
    private Button but;
    private ProgressBar xh_ProgressBar;
    private ProgressBar xh_ProgressBar2;
    private Button xh_Button;
    protected static final int GUI_STOP_NOTIFIER = 0x108;
    protected static final int GUI_THREADING_NOTIFIER = 0x109;
    public int intCounter = 0;
    private Handler handler = new Handler();

    //  该模拟填充长度为100的数组
    private int[] data = new int[100];
    private int hasdata = 0;
    //  记录ProgressBar的完成进度
    private static final int MAX_VALUE=100;
    int status = 0;
    private ProgressBar progressBarHorizontal;
    private int i = 0;

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            i++;
            if (i == 100) {
                handler.removeCallbacks(this);
            }
            progressBarHorizontal.setProgress(i);
            handler.postDelayed(this, 100);
        }
    };


    private CountDownTimer timer = new CountDownTimer(10000, 1000) {
        @Override
        public void onTick(long l) {
            tv.setText(l / 1000 + "秒 后重发");
        }

        @Override
        public void onFinish() {
            tv.setEnabled(true);
            tv.setText("正在更新");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗口模式，因为需要显示进度条在标题栏
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setProgressBarVisibility(true);
        setContentView(R.layout.activity_time3);
        init();//初始化
        timer.start();//倒计时
        setProgressBar();//进度条
        setProgressBar2();//进度条2
        setProgressBar3();//进度条3  progressBarHorizontal



    }

    /**
     * 进度条3
     */
    private void setProgressBar3() {
        handler.postDelayed(runnable,1000);
    }

    /**
     * 进度条2
     */
    private void setProgressBar2() {
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final ProgressDialog proDia=new ProgressDialog(TimeActivity3.this);
                //设置水平条的格式为水平
                proDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //设置最大值
                proDia.setMax(MAX_VALUE);
                //设置进度的开始数值,从30开始
                proDia.setProgress(30);
                proDia.onStart();
                //匿名内部类
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        for(int i=0;i<MAX_VALUE;i++){
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            proDia.incrementProgressBy(1);
                        }
                        //匿名内部类要访问类当中的数据，该数据必须为final
                        proDia.dismiss();//隐藏对话框
                    }
                }.start();
                proDia.show();
            }
        });
    }

    /**
     * 进度条
     */
    private void setProgressBar() {
        //设置进度条 不为 不确定模式
        xh_ProgressBar.setIndeterminate(false);
        xh_ProgressBar2.setIndeterminate(false);

        xh_Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //设置ProgressBar为可见状态
                xh_ProgressBar.setVisibility(View.VISIBLE);
                xh_ProgressBar2.setVisibility(View.VISIBLE);
                //设置ProgressBar的最大值
                xh_ProgressBar.setMax(100);
                //设置ProgressBar当前值
                xh_ProgressBar.setProgress(0);
                xh_ProgressBar2.setProgress(0);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 10; i++) {
                            try {
                                intCounter = (i + 1) * 20;
                                Thread.sleep(1000);
                                if (i == 4) {
                                    Message m = new Message();

                                    m.what = TimeActivity3.GUI_STOP_NOTIFIER;
                                    TimeActivity3.this.myMessageHandler
                                            .sendMessage(m);
                                    break;
                                }

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();

            }
        });

    }

    Handler myMessageHandler = new Handler() {
        private WeakReference<TimeActivity3> activity;

        void myMessageHandler(WeakReference<TimeActivity3> activity) {
            this.activity = activity;
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                //ProgressBar已经是最大值
                case TimeActivity3.GUI_STOP_NOTIFIER:
                    xh_ProgressBar.setVisibility(View.GONE);
                    xh_ProgressBar2.setVisibility(View.GONE);
                    Thread.currentThread().interrupted();
                    break;
                case TimeActivity3.GUI_THREADING_NOTIFIER:
                    if (!Thread.currentThread().isInterrupted()) {
                        //改变ProgressBar的当前值
                        xh_ProgressBar.setProgress(intCounter);
                        xh_ProgressBar2.setProgress(intCounter);

                        //设置标题栏中前景的一个进度条进度值
                        setProgress(intCounter * 100);
                    }
                    break;
                case 0x111:
                    activity.get().bar.setProgress(activity.get().status);
                    break;
            }
            super.handleMessage(msg);
        }
    };
    //  负责更新进度


    /**
     * 初始化
     */
    private void init() {
        tv=findViewById(R.id.time);
        bar=findViewById(R.id.bar);
        but=findViewById(R.id.mybut);
        //得到ProgressBar
        xh_ProgressBar = (ProgressBar) findViewById(R.id.ProgressBar01);
        xh_ProgressBar2 = (ProgressBar) findViewById(R.id.ProgressBar02);
        xh_Button = (Button) findViewById(R.id.Button01);
        progressBarHorizontal=findViewById(R.id.progressBarHorizontal);

    }
}